package com.atguigu.servicebase.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Date;


@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        // 或者
//        this.strictInsertFill(metaObject, "gmtCreate", LocalDateTime.class, LocalDateTime.now() ); // 起始版本 3.3.0(推荐)
//        this.strictInsertFill(metaObject, "gmtModified", LocalDateTime.class, LocalDateTime.now() );

        //属性名称，不是字段名称
        this.setFieldValByName("gmtCreate", new Date(), metaObject);
        this.setFieldValByName("gmtModified", new Date(), metaObject);

//
//        this.fillStrategy(metaObject, "gmtCreate", LocalDateTime.now());
//        this.fillStrategy(metaObject, "gmtModified", LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {


        // 或者
//        this.strictUpdateFill(metaObject, "gmtModified", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
//        this.fillStrategy(metaObject, "gmtModified", LocalDateTime.now());

        this.setFieldValByName("gmtModified", new Date(), metaObject);

    }
}
